// 引入所需的模块
const express = require('express');
const bodyParser = require('body-parser');

// 创建Express应用
const app = express();
const port = 8086;

// 使用body-parser中间件解析POST请求的数据
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// 定义会员对象
class Member {
    constructor(name, email, phoneNumber) {
        this.name = name;
        this.email = email;
        this.phoneNumber = phoneNumber;
    }
}

// 创建一个会员管理系统
const memberManagementSystem = {
    members: [],

    addMember: function(name, email, phoneNumber) {
        const member = new Member(name, email, phoneNumber);
        this.members.push(member);
        return this.members;
    },

    deleteMember: function(email) {
        this.members = this.members.filter(member => member.email !== email);
        return this.members;
    },

    updateMemberInfo: function(email, newPhoneNumber) {
        this.members.forEach(member => {
            if (member.email === email) {
                member.phoneNumber = newPhoneNumber;
            }
        });
        return this.members;
    },

    getMembers: function() {
        return this.members;
    }
};

// 处理添加会员请求
app.post('/addMember', (req, res) => {
    const { name, email, phoneNumber } = req.body;
    const updatedMembers = memberManagementSystem.addMember(name, email, phoneNumber);
    res.json(updatedMembers);
});

// 处理删除会员请求
app.delete('/deleteMember/:email', (req, res) => {
    const email = req.params.email;
    const updatedMembers = memberManagementSystem.deleteMember(email);
    res.json(updatedMembers);
});

// 处理更新会员信息请求
app.put('/updateMemberInfo/:email', (req, res) => {
    const email = req.params.email;
    const newPhoneNumber = req.body.newPhoneNumber;
    const updatedMembers = memberManagementSystem.updateMemberInfo(email, newPhoneNumber);
    res.json(updatedMembers);
});

// 处理获取所有会员请求
app.get('/members', (req, res) => {
    const members = memberManagementSystem.getMembers();
    res.json(members);
});

// 启动服务器
app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
    });